package com.soccertgc.logging
{
	import com.soccertgc.core.DataObject;

	public class LogOptions extends DataObject implements IDumpable
	{
		
		// -------------------------------------------------------------------------------
		// CONSTANTS
		// -------------------------------------------------------------------------------
		
		// JSON Property names
		protected const		JSON_PROPERTY_TARGETS:String			= "targets";
		
		// Default debug options
		public static const DEFAULT:LogOptions		 				= new LogOptions( {	targets: 	[	LogTargetParams.TRACE_TARGET_DATA,
																										LogTargetParams.TEXTAREA_TARGET_DATA /*,
																										LogTargetParams.REMOTE_TARGET_DATA*/
																								 	]
																					  });
		
		// -------------------------------------------------------------------------------
		// FIELDS
		// -------------------------------------------------------------------------------
		
		private var _targets:Vector.<LogTargetParams>;
		
		// -------------------------------------------------------------------------------
		// CONSTRUCTOR
		// -------------------------------------------------------------------------------
		
		public function LogOptions(data:Object = null)
		{
			// Init objects
			_targets = new Vector.<LogTargetParams>();
			// Clear data
			clear();
			// If data is provided, initializes the instance
			if (data != null) load(data);
		}
		
		// -------------------------------------------------------------------------------
		// PROPERTIES
		// -------------------------------------------------------------------------------
				
		public function get targets():Vector.<LogTargetParams>
		{
			return _targets;
		}
		
		// -------------------------------------------------------------------------------
		// PRIVATE METHODS
		// -------------------------------------------------------------------------------
		
		// DataObject implementation
		
		override protected function fill(data:Object):void
		{
			if (!data.hasOwnProperty(JSON_PROPERTY_TARGETS)) 	throwLoadMissingPropError(JSON_PROPERTY_TARGETS);
		}
		
		override protected function validate(data:Object):void
		{
			// Log options
			var raw_options:Array = data[JSON_PROPERTY_TARGETS];
			var options:Vector.<LogTargetParams> = new Vector.<LogTargetParams>();
			// Create temporary log options list
			for (var i:int = 0; i < raw_options.length; i++)
			{
				options.push(new LogTargetParams(raw_options[i]));
			}
			// 
			_targets.splice(0, _targets.length); 
			for (var j:int = 0; j < raw_options.length; j++)
			{
				_targets.push(options[j]);
			}
		}
		
		// -------------------------------------------------------------------------------
		// PUBLIC METHODS
		// -------------------------------------------------------------------------------
		
		public function getOption(index:int):LogTargetParams
		{
			if (index < 0 || index >= _targets.length) throw new Error("Index of of bounds");
			return _targets[index];
		}
		
		public function addOption(logOption:LogTargetParams):void
		{
			_targets.push(logOption);
		}
		
		public function removeOptionAt(index:int):void
		{
			_targets.splice(index, 1);
		}
		
		public function equals(logOptions:LogOptions):Boolean
		{
			if (this === logOptions) return true;
			if (_targets.length != logOptions.targets.length) return false;
			for (var i:int = 0; i < _targets.length; i++)
			{
				if (!_targets[i].equals(logOptions[i])) return false;
			}
			return true;
		}
		
		override public function clear():void
		{
			_targets.splice(0, _targets.length);
		}
		
		public function dump(source:Array = null, indentDepth:int = 0):Array
		{
			return Dumper.dump(this, null, source, indentDepth);
		}
		
	}
}